package com.yanqu.road.server.cmd.activity.ppl;

import com.yanqu.road.entity.activity.ppl.PplShootResult;
import com.yanqu.road.entity.activity.ppl.PplUserData;
import com.yanqu.road.entity.activity.ppl.shootlogic.PplScreenPos;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.pb.PplPb;
import com.yanqu.road.pb.activity.PplProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ppl.PplModule;
import com.yanqu.road.server.manger.activity.ppl.PplMgr;
import com.yanqu.road.server.manger.activity.ppl.PplShootLogic;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.math.BigDecimal;
import java.util.List;

/**
 * 新泡泡龙发射球
 */
@Cmd(code = GameProtocol.S_ACTIVITY_PPL_NEW_SHOOT_BALL, desc = "新泡泡龙发射球")
public class NewShootBallCmd extends AbstractCommand {

    private int angleFromClient;
    private int height;
    private int multiType = 1;

    private PplUserData userData;

    private List<PplScreenPos> moveKeyPos;
    private PplShootResult result;
    private boolean outScreen = false;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        PplProto.NewShootBallReqMsg reqMsg = PplProto.NewShootBallReqMsg.parseFrom(packet.getBytes());
        angleFromClient = reqMsg.getAngle();
        height = reqMsg.getHeight();
        if (reqMsg.hasMultiType()) {
            multiType = reqMsg.getMultiType();
        }
        // 角度不能超出范围，否则自动调整
        if (angleFromClient < -75) {
            angleFromClient = -75;
        }
        if (angleFromClient > 75) {
            angleFromClient = 75;
        }
        // 高度也有限制
        if (height < 800) {
            height = 800;
        }
        if (height > 1800) {
            height = 1800;
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (multiType != 1 && multiType != 2 && multiType != 3) {
            ret = GameErrorCode.E_ACTIVITY_PPL_MULTI_TYPE_ERROR;
            return;
        }
        if (!PplMgr.isActivityInTime()) {
            ret = GameErrorCode.E_ACTIVITY_PPL_NO_IN_TIME;
            return;
        }

        // 旧配置不能用16倍
        if (multiType == 3 && PplMgr.FAST_UNLOCK_VIP_LEVEL == Integer.MAX_VALUE) {
            ret = GameErrorCode.E_DRINK_ACTIVITY_NO_UNLOCK_MULTI;
            return;
        }

        PplModule pplModule = player.getModule(PplModule.class);
        userData = pplModule.getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_ACTIVITY_PPL_SYSTEM_NO_UNLOCK;
            return;
        }
        if (multiType == 2 && userData.getScore() < PplMgr.MULTI_UNLOCK_SCORE) {
            ret = GameErrorCode.E_ACTIVITY_PPL_SCORE_NO_ENOUGH;
            return;
        }
        if (multiType == 3 && VipMgr.getVipLv(player.getModule(PlayerModule.class).getUserInfo().getVipExp()) < PplMgr.FAST_UNLOCK_VIP_LEVEL) {
            ret = GameErrorCode.E_ACTIVITY_PPL_SCORE_NO_ENOUGH;
            return;
        }

        // 切换倍率
        userData.changeMulti(multiType);

        pplModule.checkEnergyRecovery();
        pplModule.checkMaxY();
        if (!pplModule.isBeautyChosen()) {
            ret = GameErrorCode.E_ACTIVITY_PPL_BEAUTY_NO_CHOSEN;
            return;
        }
        int needEnergy = multiType == 3 ? 16
                : multiType == 2 ? 4
                : 1;
        if (!pplModule.isEnergyEnough(needEnergy)) {
            ret = GameErrorCode.E_ACTIVITY_PPL_ENERGY_NO_ENOUGH;
            return;
        }
        if (PplMgr.isGameOver(pplModule.getUserData(), multiType)) {
            ret = GameErrorCode.E_ACTIVITY_PPL_HAD_GAME_OVER;
            return;
        }

        // 获取路径：[弹射点]+被碰撞点+碰撞时自身的点+最终停留点
        // 如果飞出屏幕，则路径：[弹射点]+屏幕外的点
        moveKeyPos = PplShootLogic.getMoveKeyPos(new BigDecimal(angleFromClient), pplModule.getUserData(), BigDecimal.valueOf(height));

        // 飞出屏幕，则特殊处理
        if (moveKeyPos.size() > 0 && moveKeyPos.get(moveKeyPos.size() - 1).getScreenY().compareTo(BigDecimal.ZERO) < 0) {
            outScreen = true;
            ret = pplModule.shootBallOutScreen();
            return;
        }

        if (moveKeyPos.size() < 3) {
            ret = GameErrorCode.E_ACTIVITY_PPL_POSITION_ERROR;
            return;
        }
        PplScreenPos stopPos = moveKeyPos.get(moveKeyPos.size() - 1);

        result = pplModule.shootBall(stopPos.getX(), stopPos.getY());
        ret = result.getRet();

        // 保存倍率修改
        userData.saveMultiData();
    }

    @Override
    public void sendData(GamePlayer player) {
        PplProto.NewShootBallRespMsg.Builder builder;

        // 飞出屏幕，则特殊处理
        if (ret == 0 && outScreen) {
            builder = PplPb.parseNewShootBallOutScreenRespMsg(player.getModule(PplModule.class).getUserData(), moveKeyPos);
            player.sendPacket(ClientProtocol.U_ACTIVITY_PPL_NEW_SHOOT_BALL, builder);
            return;
        }

        if (ret == 0) {
            builder = PplPb.parseNewShootBallRespMsg(player.getModule(PplModule.class).getUserData(), result, moveKeyPos);
        } else {
            builder = PplProto.NewShootBallRespMsg.newBuilder();
            builder.setRet(ret);
        }
        player.sendPacket(ClientProtocol.U_ACTIVITY_PPL_NEW_SHOOT_BALL, builder);
    }
}
